/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package edu.ccut.saturn.component.impl;

import java.sql.Connection;
import java.util.Map;
import java.util.Set;

import org.apache.commons.jexl.JexlContext;
import org.apache.commons.jexl.JexlHelper;

import edu.ccut.saturn.component.ILogicContext;
import edu.ccut.saturn.component.IParameterInfo;
import edu.ccut.saturn.component.utils.DateUtils;

/**
 * {@link ILogicContext} implementation
 * 
 * @see ILogicContext
 * @author LeslieGu
 */
public class SaturnLogicContext extends AbstractSaturnContext {

	private Map<String, Object> values;

	private JexlContext context;
	
	@SuppressWarnings("unchecked")
	public SaturnLogicContext(String id) {
		super(id);
		this.context = JexlHelper.createContext();
		this.values = context.getVars();
	}

	@SuppressWarnings("unchecked")
	public SaturnLogicContext(String fullKey, Connection con, String type) {
		super(fullKey, con, type);
		this.context = JexlHelper.createContext();
		this.values = context.getVars();
	}

	public Object getValue(String key) {

		if (super.isFinalValueKey(key)) {
			return key.substring(1, key.length() - 1);
		}

		if (super.isExpression(key)) {

			if (SATURN_CONTEXT.equals(key)) {
				return this;
			}

			if (SATURN_SYSTEM_DATE.equals(key)) {
				return DateUtils.getSystemDate();
			}

			if (SATURN_SYSTEM_TIME.equals(key)) {
				return DateUtils.getSystemTime();
			}
			return super.getExpression(key);
		}
		return this.values.get(key);
	}

	public void setValue(String key, Object value) {
		this.values.put(key, value);
	}

	public Object getValue(IParameterInfo parameterInfo) {

		if (parameterInfo != null) {
			return this.getValue(parameterInfo.getValueKey());
		}
		return null;
	}

	public void setValue(IParameterInfo parameterInfo, Object value) {

		if (parameterInfo != null) {
			this.setValue(parameterInfo.getValueKey(), value);
		}
	}

	@Override
	public JexlContext getJexlContext() {
		return this.context;
	}

	@Override
	public Set<String> getValueKeys(int scope) {
		return this.values.keySet();
	}

	@Override
	public void cleanScope(int scope) {
		this.cleanLogicScope();
	}

	private void cleanLogicScope() {
		this.values.clear();
	}

	public void removeParameterKey(String key, int scope) {
		this.removeParameterKey(key);
	}

	private void removeParameterKey(String key) {
		this.values.remove(key);
	}
	
	public ILogicContext newLogicContext() {
		SaturnLogicContext context = new SaturnLogicContext(super.getId());
		
		for (String key : this.values.keySet()) {
			context.values.put(key, this.values.get(key));
		}
		
		return context;
	}

	public Object getValue(String key, int scope) {
		if (scope == IParameterInfo.SCOPE_LOGIC) {
			return this.getValue(key);
		}
		
		return null;
	}
}
